package com.andova.game

import android.content.Context
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.andova.opengles.R
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class SampleGameActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val glsv = GLSurfaceView(this)
        setContentView(glsv)
        glsv.setEGLContextClientVersion(3)
        glsv.setRenderer(SampleGameRender(this, BitmapFactory.decodeResource(resources, R.drawable.air_hockey_surface)))
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (!hasFocus) return
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }
}

class SampleGameRender(private val context: Context, private val bitmap: Bitmap) : GLSurfaceView.Renderer {
    init {
        System.loadLibrary("gles-sample")
    }

    override fun onDrawFrame(gl: GL10?) = run { onDrawFrame() }
    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) = run { onSurfaceCreated(context.assets, bitmap) }
    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) = run { onSurfaceChanged(width, height) }
    external fun onSurfaceCreated(assetManager: AssetManager, bitmap: Bitmap)
    external fun onSurfaceChanged(w: Int, h: Int)
    external fun onDrawFrame()
}